/**
* 某板载控制芯片的控制软件
*/
#include <stdio.h>
#include "string.h"

#include "pindefine.h"
#include "bmc_control.h"
#include "i2c_comu.h"
#include "bsp_usart.h"
#include "systick.h"
#include "log_record.h"
#include "adc_voltage.h"
#include "bsp_resetirq.h"

#define VERSION (1.3)

int waitTimer=0, tempaureTImer=0, adcTimer=0, abnormalVoltageTimer = 0;
int enable_restIntTimer=0;
int rebootCont=0;
extern int isInWait;
extern bmc_comu_status_data bmc_status_data;
uint32_t Timer = 0;

extern A7_Comu a7_Comu;
extern uint8_t comuFlag;

extern int logTimer;
extern char logBuf[51200];
extern int logBufLength;
extern char logSendBuf[51200];
extern uint8_t vtgNeedRead;
extern uint8_t logNeedRead;
/*

板卡使用的外部晶振频率为25MHz

对system_gd32f4xx.c文件中将__SYSTEM_CLOCK_120M_PLL_25M_HXTTAL

在程序初始化时 程序会自动调用SystemInit函数 并在clockCongfig中调用25Mhz外援变频120Mhz的函数

温度检测每5S检测一次

板卡位置采集 100ms检测一次
*/

int main(){
	
	bmc_system_init();
	
	systick_config();
	
	bmc_devices_power_on();
	
	init_usart_port();
	
	I2C_Init(I2C2);
	
	init_usart_memo_port();
	
	init_usart_7A_port();
	
	adc_init();
	
	log_init();
	
	bmc_devices_reset();
	
	initResetExti();
		
	float ftmp;
	
	printf("\r\nBMC_VERSION: %.2f\r\n", VERSION);
	
	
		/* ========= add second power on,  20221113*/
	
	delay_1ms(1000);
	bmc_device_power_down();
	init_usart_port();	
	init_usart_memo_port();
	init_usart_7A_port();
	
	bmc_devices_power_on();
	bmc_devices_reset();

	delay_1ms(2000);

	//============
	
	
	
	while(1){
		if(enable_restIntTimer>600)
		{
			enableResetExti();
		}
		
		if(getResetStatusFlag()>0)
			//when ResetFlag enable clear all port output 
			//delay 5s
			//restart power on
		{
			enable_restIntTimer=0;	
			disableResetExti();
			clearResetStatusFlag();
			if(rebootCont<1)
			{
				bmc_pc_restart();
				rebootCont++;
			}
		}
		
		if(isInWait==2)
		{
			usart_data_transmit(UART6, '3');
			waitTimer+=5;
			
			if(waitTimer==500)
			{
				bmc_reset_board();
				waitTimer=0;
				isInWait=0;
			}
		}
		
		comuBoardCommand();
				
		if(comuFlag==1)
		{
			comuFlag=0;
			
			printf("\r\n%#x %#x %#x %#x\r\n", a7_Comu.head,a7_Comu.data, a7_Comu.recmd, a7_Comu.foot);
			
			if(a7_Comu.head==0x5A && a7_Comu.foot==0xA5 && a7_Comu.data == a7_Comu.recmd)
			{
				printf("\r\nBMC: get 7A Command %#x\r\n", a7_Comu.data);
				switch(a7_Comu.data)
				{
					//获取板卡位置
					case 0x11:
					{
 
						A7_Comu sendA7_comu;
						sendA7_comu.head=0x5B;
						sendA7_comu.foot=0xB5;
						sendA7_comu.recmd=0x11;
						sendA7_comu.data=getBoardLocation();
						printf("\r\nBMC: BoardLocation = %d\r\n",sendA7_comu.data);
						for(int i=0;i<sizeof(sendA7_comu);i++)
						{
							usart_data_transmit(UART3, ((char*)&sendA7_comu)[i]);
							while(RESET==usart_flag_get(UART3,USART_FLAG_TBE));
						}
					}
						break;
				}
			}
			else
			{
				printf("\r\nBMC: get 7A Comand Error\r\n");
			}
		}

		
		if(tempaureTImer > 1000)
		{
			bmc_getAllTemperatureData();
			printf("%f %f %f\r\n", covert_lm75_2_tempature(bmc_status_data.tempuerature_3A3000), covert_lm75_2_tempature(bmc_status_data.tempuerature_7A1000),
																				covert_lm75_2_tempature(bmc_status_data.tempuerature_GPU));
			ftmp = Timer * 1.0f / 200.0f;
			temperature_check(ftmp, covert_lm75_2_tempature(bmc_status_data.tempuerature_3A3000), covert_lm75_2_tempature(bmc_status_data.tempuerature_7A1000), covert_lm75_2_tempature(bmc_status_data.tempuerature_GPU));
			tempaureTImer=0;
		}
		else
		{
//			tempaureTImer++;
		}
		
		if(adcTimer > 200)
		{
//			voltage_printf();
			adcTimer=0;
		}
		else
		{
			adcTimer++;
		}
		
		if(abnormalVoltageTimer > 1000)
		{
			ftmp = Timer * 1.0f / 200.0f;
			voltage_check(ftmp);
			abnormalVoltageTimer=0;
		}
		else
		{
			abnormalVoltageTimer++;
		}
		
		if(vtgNeedRead != 0)		//读取本次开机记录的异常电压
		{
			readCurrentAbnormalVoltage();
			vtgNeedRead = 0;
		}
		
		if(logNeedRead != 0)		//读取Flash日志
		{
			//readCurrentLog();
			readAllLogAndVtg();
			logNeedRead = 0;
		}
		
		if( logTimer > 9000 )
		{
			if(logBufLength != 0)
			{
				printf("\r\n******** Start to Write Log to Flash. Lenght = %d Bytes ********\r\n", logBufLength);
				memcpy(logSendBuf, logBuf, logBufLength);
				int lgth = logBufLength;
				logBufLength = 0;
				writeLog((uint8_t *)logSendBuf, lgth);
				printf("******** End of Writing Log to Flash ********\r\n");
			}
			logTimer = 0;
		}
		else
		{
			logTimer++;
		}
		
		delay_1ms(5);
		++Timer;
		++enable_restIntTimer;
	}
}

int fputc(int ch, FILE *f)
{
	usart_data_transmit(UART6, (uint8_t)ch);
	while(RESET==usart_flag_get(UART6, USART_FLAG_TBE));
	
	return ch;
}
